<?php
	class baivietdb
	{
		public function Them($MaLoaiChuyenMuc = 0,
							 $TieuDe = "",
							 $TomTat = "",
							 $HinhAnh="",
							 $TenHinh="",
							 $NoiDung = "",
							 $NgayDang = "",
							 $LuotXem = 0,
							 $XuatBan = 0)
		{
			$sql = "INSERT INTO baiviet(MaLoaiChuyenMuc, TieuDe, TomTat, HinhAnh, TenHinh, NoiDung, NgayDang, LuotXem, XuatBan) 
					VALUES(".dataservice::QuoteSmart($MaLoaiChuyenMuc).", 
							".dataservice::QuoteSmart($TieuDe).", 
							"."'".$TomTat."', 
							".dataservice::QuoteSmart($HinhAnh).", 
							".dataservice::QuoteSmart($TenHinh).", 
							"."'".$NoiDung."', 
							".dataservice::QuoteSmart($NgayDang).", 
							"."0,
							".dataservice::QuoteSmart($XuatBan).")";
			//echo $sql;
			$data = new dataservice();
			return $data->ExecuteNonQuery($sql);
		}
		
		public function LayDanhSach($MaBaiViet = 0, 
									$MaLoaiChuyenMuc = 0,
									$NgayDang = 0, 
									$LuotXem = 0,
									$XuatBan=0,
									$TieuDe = "")
									
		{
			$sql = "select TenHinh, HinhAnh, MaBaiViet, TieuDe, TomTat, ".(($MaBaiViet > 0) ? "NoiDung, " : "")." NgayDang, LuotXem, XuatBan, loaichuyenmuc.MaLoaiChuyenMuc, loaichuyenmuc.TenLoaiChuyenMuc from loaichuyenmuc, baiviet where (1) and baiviet.MaLoaiChuyenMuc = loaichuyenmuc.MaLoaiChuyenMuc";
			
			if($MaBaiViet > 0)
				$sql .= ' and MaBaiViet = '.dataservice::QuoteSmart($MaBaiViet);
			
			if($MaLoaiChuyenMuc > 0)
				$sql .= ' and baiviet.MaLoaiChuyenMuc = '.dataservice::QuoteSmart($MaLoaiChuyenMuc);
			
			if($XuatBan > 0)
				$sql .= ' and XuatBan = '.dataservice::QuoteSmart($XuatBan);
				
			if($TieuDe != "")
				$sql .= ' and TieuDe = '.dataservice::QuoteSmart($TieuDe);
				
			if($LuotXem > 0)
				$sql .= ' order by LuotXem desc';
			
			if($NgayDang > 0)
				$sql .= ' order by NgayDang desc';
			$data = new dataservice();
			return $data->ExecuteQuery($sql);
		}
		
		public function CapNhat($MaBaiViet = 0, 
								$MaLoaiChuyenMuc = 0, 
								$TieuDe = '',
								$TomTat = '',
								$HinhAnh= '',
							 	$TenHinh= '', 							 
								$NoiDung = '')
		{
			$sql = "update baiviet set MaLoaiChuyenMuc = ".dataservice::QuoteSmart($MaLoaiChuyenMuc).", 
									   TieuDe = ".dataservice::QuoteSmart($TieuDe).", 
									   TomTat = '".$TomTat."',
									   HinhAnh = ".dataservice::QuoteSmart($HinhAnh).", 
									   TenHinh = ".dataservice::QuoteSmart($TenHinh).", 						    
									   NoiDung = '".$NoiDung."' 
					where MaBaiViet = ".dataservice::QuoteSmart($MaBaiViet);		
			$data = new dataservice();
			return $data->ExecuteNonQuery($sql);
		}
		
		public function CapNhatXuatBan($MaBaiViet = 0, 
									   $XuatBan = 0)
		{
			$sql = "update baiviet set XuatBan = ".dataservice::QuoteSmart($XuatBan)." 
					where MaBaiViet = ".dataservice::QuoteSmart($MaBaiViet);		
			$db = new dataservice();
			return $db->ExecuteNonQuery($sql);
		}
		
		public function Xoa($MaBaiViet = 0)
		{
			$sql = 'delete from baiviet where (1)';
			
			if ($MaBaiViet > 0)
				$sql .= ' and MaBaiViet = '.dataservice::QuoteSmart($MaBaiViet);			
			
			$data = new dataservice();
			return $data->ExecuteNonQuery($sql);
		}
		
		public function XoaTatCa()
		{
			$sql = "DELETE FROM baiviet";			
			$data = new dataservice();
			return $data->ExecuteNonQuery($sql);
		}
	}
?>